﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JigLibX.Collision;
using PloobsEngine.Physics.XJigLib;
using PloobsEngine.Events;
using PloobsEngine.Physics;

namespace PloobsEngine.Trigger
{
    public class JigLibXTrigger : ITrigger
    {
        private JigLibXObject ghost;
        private IList<CollisionSkin> _excepts = new List<CollisionSkin>();
        IEvent<ITrigger> evt;

        public JigLibXTrigger(JigLibXObject ghost, IEvent<ITrigger> evt)
        {
            this.ghost = ghost;
            ghost.Body_.CollisionSkin.callbackFn += new CollisionCallbackFn(handleCollisionDetection);
            this.evt = evt;
        }

        public JigLibXObject CollisionExcepts
        {
            set
            {
                _excepts.Add(value.Body_.CollisionSkin);
            }
        }

        private bool handleCollisionDetection(CollisionSkin owner, CollisionSkin collidee)
        {
            if (!_excepts.Contains(collidee) && collidee.Owner !=null && !collidee.Owner.Immovable)
               Handle();

            return false;
        }

        #region ITrigger Members
        
        public void Handle()
        {
            evt.FireEvent(this);
            
        }

        
        public IPhysicObject GhostObject
        {
            get
            {
                return ghost;
            }
            set
            {
                this.ghost = value as JigLibXObject; ;
            }
        }

        #endregion

        #region ITrigger Members

        public string Name
        {
            get;
            set;

        }

        #endregion

        #region ITrigger Members

        IEvent<ITrigger> ITrigger.Event
        {
            get
            {
                return evt;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
